package cn.edu.hunau.oop.day09;

import org.junit.Test;

import java.io.*;
import java.nio.charset.Charset;

/**
 * @author hb
 * @date 2025-08-14 16:25:10
 */
public class TestCharStream {

    @Test
    public void testWrite() throws IOException {
        try(
        FileOutputStream fos=new FileOutputStream("dir/a001.txt",true);
        OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8");
        BufferedWriter bw=new BufferedWriter(osw);){
            bw.newLine();
            bw.write("要下雨了。。");
            bw.newLine();
            bw.write("暑假块结束了...");
            bw.newLine();
            bw.flush();
        }
    }

    @Test
    public void testReader(){
        try(  FileInputStream fis=new FileInputStream("dir/a001.txt");
              InputStreamReader isr=new InputStreamReader(fis,"utf-8");
              BufferedReader br=new BufferedReader(isr);
                ){
            String data = null ;
            while((data=br.readLine())!=null){
                System.out.println(data);
            }
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testfw() throws Exception{
        try(
           FileWriter fw=new FileWriter("dir/a001.txt", Charset.forName("utf-8"),true);
        ){
            fw.write("\n");
            fw.write("FileWriter写入的内容");
            fw.write("\n");
            fw.flush();
        }
    }

    public void testFr()throws Exception{
        try(
        FileReader fr=new FileReader("dir/a001.txt",Charset.forName("utf-8"));
        ){

        }
    }

    @Test
    public void testPw(){
        try(
         FileOutputStream fos=new FileOutputStream("dir/a001.txt",true);
         PrintWriter pw=new PrintWriter(fos,true,Charset.forName("utf-8"));
        ){
            pw.println("PrintWriter写入的内容");
            pw.println("");
            pw.flush();
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }
}
